package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.Analysis;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashboardService {

    @Autowired
    private AnalysisMapper analysisMapper;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 后台首页概要统计
     * @return
     */
    public Map<String,Integer> summary() throws ParseException {
        //1、定义查询的日期
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesdayStr = DateUtil.yesterday().toString("yyyy-MM-dd");
        //1、当天日期的数据
        QueryWrapper<Analysis> qw = new QueryWrapper<Analysis>();
        qw.eq("record_date",new SimpleDateFormat("yyyy-MM-dd").parse(todayStr));
        Analysis analysis = analysisMapper.selectOne(qw);
        if (analysis != null) {
            Integer cumUser = userInfoApi.findAlls();
            //2、统计数据-注册数量
            Integer numRegistered = analysis.getNumRegistered();
            //3、统计数据-登录数量
            Integer numLogin = analysis.getNumLogin();
            //4、统计数据-活跃数量
            Integer numActive = analysis.getNumActive();
            //5、统计数据-次日留存
            Integer numRetention1d = analysis.getNumRetention1d();
            Map<String,Integer> map = new HashMap();
            map.put("cumulativeUsers",cumUser);
            map.put("activePassMonth",numActive);
            map.put("activePassWeek",numActive);
            map.put("newUsersToday",numRegistered);
            map.put("newUsersTodayRate",numRegistered);
            map.put("loginTimesToday",numLogin);
            map.put("loginTimesTodayRate",numLogin);
            map.put("activeUsersToday",numActive);
            map.put("activeUsersTodayRate",numActive);
            return map;
        }
        //将数据返回
        Map map = new HashMap();
        return map;
    }
    /**
     * 新增、活跃用户、次日留存率
     */
    public Map<String, Map> users(String type) {
        Map map = new HashMap();
        Map<String,Integer> map2 = new HashMap();
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        map2.put(type,100);
        map.put("thisYear",map2);
        String yesdayStr = DateUtil.yesterday().toString("yyyy-MM-dd");
        Map<String,Integer> map1 = new HashMap();
        map2.put(type,70);
        map.put("lastYear",map1);
        return map;
    }
}
